package com.knife.blog.web.action;

import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.blog.domain.Music;
import com.knife.blog.logic.BlogMusicService;
import com.knife.blog.logic.impl.BlogMusicServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogMusicAction extends BaseCrudAction {
	private static BlogMusicService musicService = BlogMusicServiceImpl.getInstance();

	@Override
	public IPageList doQuery(WebForm form, int currentPage,int pageSize) {
		return musicService.queryBlogMusic(this.getCurrentUser(form).getUserName(),"",currentPage,pageSize);
	}
	
	public Page doAdd(WebForm form, Module module, IActiveUser user){
		Music music = (Music)form2Obj(form);
		music.setInputuser(user.getUserName());
		
		boolean ret = musicService.addMusic(music);
		if (ret) {
			form.addResult("msg", "添加成功!");
			return super.doQuery(form, module, user);
		} else {
			form.addResult("msg", "添加失败!");
			return module.findPage("edit");
		}	
	}
	
	public Page doDel(WebForm form, Module module, IActiveUser user){
		boolean ret = musicService.delMusic((String)form.get("cid"));
		form.addResult("msg", ret ? "删除成功!" : "删除失败!");
		return super.doQuery(form, module, user);
	}
	
	public Page doDelBatch(WebForm form, Module module, IActiveUser user){
		int ret = musicService.batchDelMusic((String[])form.get("cid"));
		form.addResult("msg", ret==0 ? "删除"+ret+"项成功!" : "删除失败!");
		return super.doQuery(form, module, user);
	}
	
	public Page doUpdate(WebForm form, Module module, IActiveUser user){
		boolean ret = musicService.updateMusic((Music)form2Obj(form));
		form.addResult("msg", ret ? "更新成功!" : "更新失败!");
		return super.doQuery(form, module, user);
	}
	
	public Page doSetBackMusic(WebForm form, Module module, IActiveUser user){
		
		return null;
	}

	@Override
	public Object form2Obj(WebForm form) {
		String cid = CommUtil.null2String(form.get("cid"));
		Music obj = musicService.getMusic(cid);
		if(obj == null){
			obj = new Music();
		}
		form.toPo(obj);
		return null;
	}

}
